package com.rmkj.trade.match.core;

/**
 * Created by Administrator on 2019/1/8.
 */
public class DefaultMatchRule implements MatchRule {

	@Override
	public OrderPairs match(Goods goods, String lastPrice, Order seller, Order buyer) {
		double sellPrice = Double.parseDouble(seller.getPrice());
		double buyPrice = Double.parseDouble(buyer.getPrice());
		double _lastPrice = lastPrice == null ? 0 : Double.parseDouble(lastPrice);
		if (sellPrice <= buyPrice) {
			double amount = Math.min(seller.getAmount(), buyer.getAmount());
			double price = 0;
			if (lastPrice == null) {
				price = sellPrice;
			}else if(_lastPrice >= sellPrice && _lastPrice <= buyPrice){
				price = _lastPrice;
			}else if(_lastPrice < sellPrice){
				price = sellPrice;
			}else if(_lastPrice > buyPrice){
				price = buyPrice;
			}
			OrderPairs orderPairs = new OrderPairs();
			orderPairs.setAmount(amount);
			orderPairs.setPrice(goods.parse(price));
			return orderPairs;
		}
		return null;
	}

}
